//
//  ModifyGuesturePasswordController.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/6/29.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "ModifyGuesturePasswordController.h"
#import "UIUtil.h"
#import <Security/Security.h>
#import <CoreFoundation/CoreFoundation.h>

#import "KeychainItemWrapper.h"
#import "CAccountMgr.h"
#import "CAccountModel.h"
#import "MicroDef.h"
#import "LoginViewController.h"
#import "CUserSettingMgr.h"
#import "CUserSettingModel.h"
//#import "CAccountModel.h"
//#import "CAccountMgr.h"
#import "CFolderMgr.h"

#define KMaxErrorTimes 3

@interface ModifyGuesturePasswordController ()

{
    NSInteger _errorTimes;
}

@property (nonatomic,strong) GesturePasswordView * gesturePasswordView;
@property (nonatomic, strong) CUserSettingModel *setting;
@property (nonatomic, strong) CAccountModel *acc;
@end

@implementation ModifyGuesturePasswordController{
    NSString * previousString;
    NSString * password;
}

@synthesize gesturePasswordView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"手势锁定密码"];
    _errorTimes = 0;
    // Do any additional setup after loading the view.
    previousString = [NSString string];
//    password = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].gesturePassword;
  
    [self verify];
    
    
    previousString = @"";
//    [USER_DEFAULT setBool:NO forKey:is_GuseturePassword];
    [USER_DEFAULT setBool:NO forKey:is_ForgetGuesturePassword];
    [USER_DEFAULT synchronize];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 验证手势密码
- (void)verify{
    gesturePasswordView = [[GesturePasswordView alloc]initWithFrame:CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64)];

    gesturePasswordView.forgetButton.hidden = YES;
    [gesturePasswordView.tentacleView setRerificationDelegate:self];
    [gesturePasswordView.tentacleView setStyle:1];
    gesturePasswordView.guesView.hidden = NO;
    gesturePasswordView.imgView.hidden = YES;
    gesturePasswordView.headerImageView.hidden = YES;
    [gesturePasswordView setGesturePasswordDelegate:self];
    [gesturePasswordView.errorMsg setFont:[UIFont systemFontOfSize:15.0]];
    [gesturePasswordView.errorMsg setTextColor:RGBCOLOR(0xf4, 0x62, 0x0b)];
    [self.view addSubview:gesturePasswordView];
    [gesturePasswordView.state setText:@"请绘制解锁图案"];
    //    gesturePasswordView.backgroundColor = [UIColor clearColor];
//    [gesturePasswordView.changeButton setHidden:NO];
//    [gesturePasswordView.forgetButton setHidden:YES];
    gesturePasswordView.errorMsg.hidden = YES;
//   把忘记密码给隐藏掉
    gesturePasswordView.forgetButton.hidden=YES;
}

#pragma mark - 重置手势密码
- (void)reset{
    
    
}

#pragma mark - 判断是否已存在手势密码
- (BOOL)exist{
    KeychainItemWrapper * keychin = [[KeychainItemWrapper alloc]initWithIdentifier:@"Gesture" accessGroup:nil];
    password = [keychin objectForKey:(__bridge id)kSecValueData];
    if ([password isEqualToString:@""])return NO;
    return YES;
}

#pragma mark - 清空记录
- (void)clear{
    KeychainItemWrapper * keychin = [[KeychainItemWrapper alloc]initWithIdentifier:@"Gesture" accessGroup:nil];
    [keychin resetKeychainItem];
}

#pragma mark - 改变手势密码
- (void)change{
    previousString = @"";
    gesturePasswordView.tentacleView.userInteractionEnabled = YES;
    CUserSettingMgr *setMgr = [[CUserSettingMgr alloc] init];
    self.setting.isUseGesturePassword = NO;
    [setMgr updateUserSetting:self.setting];
}

#pragma mark - 忘记手势密码
- (void)forget{
    
}

- (BOOL)verification:(NSString *)result{
    
    if(M_ISSTRNULL(result))
    {
        return NO;
    }
    
//    NSLog(@"resulyt==========%@",result);
    if (result.length >= 4) {
        
        gesturePasswordView.errorMsg.hidden = YES;
       return [self resetPassword:result];
        
    } else {
        gesturePasswordView.errorMsg.hidden = NO;
        [gesturePasswordView.errorMsg setText:@"请至少连接4个点"];
        return NO;
    }
}

- (BOOL)resetPassword:(NSString *)result{
    if (previousString.length < 4) {
        previousString=result;
        [gesturePasswordView.tentacleView enterArgin];
        //        [gesturePasswordView.state setTextColor:[UIColor colorWithRed:2/255.f green:174/255.f blue:240/255.f alpha:1]];
        [gesturePasswordView.state setText:@"请确认解锁图案"];
        return YES;
    }
    else {
        if ([result isEqualToString:previousString]) {
            KeychainItemWrapper * keychin = [[KeychainItemWrapper alloc]initWithIdentifier:@"Gesture" accessGroup:nil];
            [keychin setObject:@"<帐号>" forKey:(__bridge id)kSecAttrAccount];
            [keychin setObject:result forKey:(__bridge id)kSecValueData];
            //[self presentViewController:(UIViewController) animated:YES completion:nil];
            //            [gesturePasswordView.state setTextColor:[UIColor colorWithRed:2/255.f green:174/255.f blue:240/255.f alpha:1]];

            [USER_DEFAULT setObject:result forKey:GUESTUREPASSWORD];
            [USER_DEFAULT setBool:YES forKey:is_GuseturePassword];
            [USER_DEFAULT setBool:NO forKey:is_ForgetGuesturePassword];
            [USER_DEFAULT synchronize];
            
            [gesturePasswordView.state setText:@"已保存手势密码"];
            
            gesturePasswordView.tentacleView.userInteractionEnabled = NO;
            
            //bug 6294 fixed by 荣则兵
            [self.navigationController popViewControllerAnimated:YES];
            return YES;
        }
        else{
            previousString =@"";
            //            [gesturePasswordView.state setTextColor:[UIColor redColor]];
            [gesturePasswordView.state setText:@"您输入的密码与上次不一致"];
            [gesturePasswordView.errorMsg setHidden:YES];
            return NO;
        }
    }
}
@end
